//
//  BYBlogCell.m
//  School+
//
//  Created by Sean Sebastian on 16/5/6.
//  Copyright © 2016年 Sean Sebastian. All rights reserved.
//

#import "BYBlogCell.h"
#import "BYPicturesCollectionView.h"
#import "SVProgressHUD.h"
#import "masonry.h"



#define BlogCellMargin 8

@interface BYBlogCell ()

@property (nonatomic , weak) UIImageView *iconView;
@property (nonatomic , weak) UILabel *username_Label;
@property (nonatomic , weak) UILabel *content_Label;
@property (nonatomic , weak) BYPicturesCollectionView *pictureCollectionView;
@property (nonatomic , weak) UIButton *location_Button;
@property (nonatomic , weak) UIButton *message_Button;
@property (nonatomic , strong) NSDictionary *iconDict;



@end

@implementation BYBlogCell

- (void)prepareUI
{
    UIImageView *imageView = [[UIImageView alloc] initWithImage:[self.iconDict objectForKey:self.blog.adminName]];
    self.backgroundView = imageView;
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    UIVisualEffectView *effcetView = [[UIVisualEffectView alloc] initWithEffect:blur];
    [imageView addSubview:effcetView];
    
    [effcetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];

    
    
    [self addSubview:self.iconView];
    [self addSubview:self.username_Label];
    [self addSubview:self.content_Label];
    [self addSubview:self.pictureCollectionView];
    
    [self addSubview:self.location_Button];
    
    [self addSubview:self.message_Button];
    
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self).with.offset(10);
        make.width.and.height.mas_equalTo(@100);
    }];
    
    [self.username_Label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.mas_bottom).with.offset(5);
        make.centerX.equalTo(self.mas_centerX);
        
        
    }];
    
    
    NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:self.content_Label.text];
    
    NSRange allRange = [self.content_Label.text rangeOfString:self.content_Label.text];
    [attrStr addAttribute:NSFontAttributeName
                    value:[UIFont systemFontOfSize:13.0]
                    range:allRange];
    [attrStr addAttribute:NSForegroundColorAttributeName
                    value:[UIColor blackColor]
                    range:allRange];
    
    
    CGFloat titleHeight;
    
    NSStringDrawingOptions options =  NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading;
    CGRect rect = [attrStr boundingRectWithSize:CGSizeMake(self.content_Label.frame.size.width, CGFLOAT_MAX)
                                        options:options
                                        context:nil];
    titleHeight = ceilf(rect.size.height);
    
    _content_Label.numberOfLines = 0;
    [_content_Label sizeThatFits: rect.size];
    
    [self.content_Label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.username_Label.mas_bottom).with.offset(5);
        make.left.equalTo(self).with.offset(10);
        make.right.equalTo(self.mas_right).with.offset(-10);
        make.height.mas_equalTo(@(titleHeight + 50));

    }];
    
    [self.pictureCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.content_Label.mas_bottom).with.offset(10);
        make.left.equalTo(self.content_Label);
        
        make.width.mas_equalTo(@240);
        if (self.blog.picDicts.count == 0) {
            make.height.mas_equalTo(@0);
        }else if( self.blog.picDicts.count == 1){
            make.height.mas_equalTo(@120);make.width.mas_equalTo(@120);
        }else if ( self.blog.picDicts.count == 2){
            make.height.mas_equalTo(@120); make.width.mas_equalTo(@240);
        }else{
            make.height.mas_equalTo(@240);
        }
        
        
        
    }];
    
    
    [self.location_Button mas_makeConstraints:^(MASConstraintMaker *make) {
        if (self.blog.locationX == 0 ) {
            make.height.mas_equalTo(@0);
            make.width.mas_equalTo(@0);
            self.location_Button.hidden = YES;
        }
        
        make.top.equalTo(self.pictureCollectionView.mas_bottom).with.offset(10);
        make.left.equalTo(self.content_Label);
        make.right.equalTo(self.content_Label);
        make.height.mas_equalTo(@24);
        
        
    }];
    [self.location_Button sizeToFit];
    
    
    
    [self.message_Button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.location_Button.mas_bottom);
        make.left.equalTo(self);
        make.right.equalTo(self);
        make.height.mas_equalTo(@40);
    
    }];
    
    
    
    [self layoutIfNeeded];
    
    CGFloat h= CGRectGetMaxY(self.message_Button.frame);
    
    self.cellHeight = h + 5;
    self.blog.cellHeight = self.cellHeight;
}


- (void)setBlog:(Blog *)blog
{
    _blog = blog;
    [self prepareUI];
}

- (NSDictionary *)iconDict
{
    if (_iconDict == nil) {
        NSDictionary *dict = @{
                               @"Cena":[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://localhost/SchoolPlus/images/Admins/default_1.jpg"]]],
                               @"Vin":[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://localhost/SchoolPlus/images/Admins/default_2.jpg"]]],
                               @"Rock":[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://localhost/SchoolPlus/images/Admins/default_3.jpg"]]]
                               };
        _iconDict = dict;
        return dict;
    }
    return _iconDict;
}

- (UIImageView *)iconView
{
    if (_iconView == nil) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [self.iconDict objectForKey:self.blog.adminName];
        _iconView = imageView;
        return imageView;
    }
    return _iconView;
}

- (UILabel *)username_Label
{
    if (_username_Label == nil) {
        UILabel *label = [[UILabel alloc] init];
        label.text = self.blog.adminName;
        [label sizeToFit];
        label.backgroundColor = [UIColor lightGrayColor];
        
        _username_Label = label;
        return label;
    }
    return _username_Label;
}

- (UILabel *)content_Label
{
    if (_content_Label == nil) {
        UILabel *label = [[UILabel alloc] init];
        label.backgroundColor = [UIColor whiteColor];
        label.text = self.blog.content;
        _content_Label.numberOfLines = 0;
        [label sizeToFit];
        label.textAlignment = NSTextAlignmentLeft;
        _content_Label = label;
        return label;
    }
    return _content_Label;
}

- (BYPicturesCollectionView *)pictureCollectionView
{
    if (_pictureCollectionView == nil) {
        BYPicturesCollectionView *view = [[BYPicturesCollectionView alloc] init];
        view.blog = self.blog;
        
        view.backgroundColor = [UIColor clearColor];
        
        _pictureCollectionView = view;
        return view;
    }
    return _pictureCollectionView;
}

- (UIButton *)location_Button
{
    if (_location_Button == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:[NSString stringWithFormat:@"(%d,%d) %@" , self.blog.locationX , self.blog.locationY , self.blog.locationDetail] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"pin_Btn"] forState:UIControlStateNormal];
        btn.imageView.contentMode = UIViewContentModeLeft;
        btn.titleLabel.textAlignment = NSTextAlignmentLeft;
        
        [btn addTarget:self action:@selector(locationBtnClick) forControlEvents:UIControlEventTouchUpInside];

        [btn.layer setCornerRadius:10];
        [btn.layer setBorderWidth:1.0];
        
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 180);
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 180);
        
        btn.titleLabel.font = [UIFont systemFontOfSize: 12.0];
        
        
        _location_Button = btn;
        return btn;
    }
    return _location_Button;
}

- (void)locationBtnClick
{
    [self.delegate locationClick:self.blog];
}

- (UIButton *)message_Button
{
    if (_message_Button == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:@"评论" forState:UIControlStateNormal];
        
        [btn addTarget:self action:@selector(messageBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        btn.backgroundColor = [UIColor colorWithRed:0 green:255/204 blue:255/255 alpha:0.5];
        _message_Button = btn;
        return btn;
    }
    return _message_Button;
}

- (void)messageBtnClick
{
    [SVProgressHUD showErrorWithStatus:@"评论系统未定义！"];
}

@end
